package cn.jiuri.jchat.msg;

/**
 * @program: Jchat
 * @author: Mr.Han
 * @create: 2019-02-13 10:46
 **/

import cn.hutool.core.lang.Singleton;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.jiuri.jchat.api.API;
import cn.jiuri.jchat.common.CodeEnum;
import cn.jiuri.jchat.common.Core;
import cn.jiuri.jchat.model.Msg;
import cn.jiuri.jchat.model.User;
import java.util.Scanner;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 消息处理(自定义)
 */
public class HandleMsgImpl implements HandleMsgInterface {
    public static final String robotPath= "http://openapi.tuling123.com/openapi/api/v2";
    public static final String robotAppKey = "3b61790010c845258c489fadc581a694";
    private static Log log = LogFactory.get();

    private static Core core = Singleton.get(Core.class);
    @Override
    public void handleMsg(Msg msg) {
        ConcurrentHashMap<String, User> userMap = core.getUserMap();
        log.info("消息内容:{},消息发送人:{}",msg.getContent(),userMap.get(msg.getFromUserName())==null?"":userMap.get(msg.getFromUserName()).getNickName());
        Scanner scanner = new Scanner(System.in);
        API.sendMsg(scanner.next(),msg.getFromUserName(), CodeEnum.TEXT.getCode());
//        //封装图灵机器人
//        RobotRequest robotRequest = new RobotRequest();
//        RobotRequest.PerceptionBean perceptionBean = new RobotRequest.PerceptionBean();
//        RobotRequest.PerceptionBean.InputTextBean inputTextBean = new RobotRequest.PerceptionBean.InputTextBean();
//        robotRequest.setReqType(Constant.TEXT);
//        inputTextBean.setText(msg.getContent());
//        perceptionBean.setInputText(inputTextBean);
//        RobotRequest.UserInfoBean userInfoBean = new RobotRequest.UserInfoBean();
//        userInfoBean.setApiKey(robotAppKey);
//        userInfoBean.setUserId("123");
//        robotRequest.setPerception(perceptionBean);
//        robotRequest.setUserInfo(userInfoBean);
//        //ai回复
//        String body = HttpUtil.createPost(robotPath).body(new JSONObject(robotRequest)).execute().body();
//        RobotResponse robotResponse = JSONUtil.toBean(body, RobotResponse.class);
//        //回复给发送人
//        API.sendMsg(robotResponse.getResults().get(0).getValues().getText(),msg.getFromUserName(), CodeEnum.TEXT.getCode());
    }
}
